package progress.exa28_1.part4;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 学会使用Executors类来快速创建线程池并使用
 */
public class ThreadPoolStudy {

    public static void main(String[] args) {

        //创建一个线程池，且里面最多容纳1个线程
        ExecutorService executor = Executors.newFixedThreadPool(1);

        //添加任务到线程池中
        executor.execute(new PrintChar('a',5));
        executor.submit(new PrintChar('b',5));

        //关闭线程池
        executor.shutdown();

    }

    /**
     * 任务定义
     */
    static class PrintChar implements Runnable {

        private final char charToPrint;
        private final int times;

        public PrintChar(char c,int t) {
            charToPrint=c;
            times=t;
        }

        public void run() {
            for(int i=0;i<times;i++) {
                System.out.print(charToPrint);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
